<?php
    session_start();
    include('top.php');
?>
    <style>
        #alert {
            display: none;
        }
    </style>
    <div class="admin_login">
        <h2>第二学士学位招生系统管理登入</h2>
        <form id="loginForm" method="post">
            <p class="admin_p">管理员用户：
                <input type="text" name="name">
            </p>
            <p class="admin_p">管理员密码：
                <input type="password" name="password">
            </p>
            <p class="admin_p">
                <input type="submit" value="登入" class="my-home-submit">
            </p>
        </form>
    </div>
    <div class="ui_div_border" id="ui_div_border">
        <table class="ui_border ui_state_visible ui_state_focus ui_state_lock" id="alert">
            <tbody>
                <tr>
                    <td class="ui_lt"></td>
                    <td class="ui_t"></td>
                    <td class="ui_rt"></td>
                </tr>
                <tr>
                    <td class="ui_l"></td>
                    <td class="ui_c">
                        <div class="ui_inner">
                            <table class="ui_dialog">
                                <tbody>
                                    <tr>
                                        <td colspan="2">
                                            <div class="ui_title_bar">
                                                <div class="ui_title" unselectable="on" style="cursor: move;">警告</div>
                                                <div class="ui_title_buttons"><a class="ui_close" href="javascript:void(0);" title="关闭(esc键)" style="display: inline-block;">×</a></div>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="ui_icon" style=""><img src="/static/img/alert.gif" class="ui_icon_bg"></td>
                                        <td class="ui_main" style="width: 200px; height: auto;">
                                            <div class="ui_content" style="padding: 10px;" id="message_alert"></div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">
                                            <div class="ui_buttons" style="">
                                                <input type="button" value="确定" class="ui_state_highlight" onclick="location.href='home.php';">
                                            </div>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </td>
                    <td class="ui_r"></td>
                </tr>
                <tr>
                    <td class="ui_lb"></td>
                    <td class="ui_b"></td>
                    <td class="ui_rb" style="cursor: auto;"></td>
                </tr>
            </tbody>
        </table>
    </div>
    <script type="text/javascript">
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();

            var formData = new FormData(this);

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'login.php', true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {

                    var response = JSON.parse(xhr.responseText);

                    document.getElementById('message_alert').innerHTML = response.message;

                    document.getElementById('alert').style.display = 'table';
                }
            };
            xhr.send(formData);
        });

        const closeButton = document.querySelector('.ui_close');
        const confirmButton = document.querySelector('.ui_state_highlight');
        const uiDivBorder = document.getElementById('ui_div_border');

        closeButton.addEventListener('click', function() {
            uiDivBorder.style.visibility = 'hidden';
        });

        confirmButton.addEventListener('click', function() {
            uiDivBorder.style.visibility = 'hidden';
        });

        function handleResponse(response) {
            if (response.success) {
                // 显示成功消息
                alert(response.message);
                // 执行页面跳转
                window.location.href = response.redirect;
            } else {
                // 显示错误消息
                alert(response.message);
            }
        }
    </script>
    </body>

    </html>